package com.zhy.generator.config;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;

import java.io.File;
import java.util.Map;

public class ConfigurationBuilder {

    /** 根目录 */
    private String basePath;

    /** 项目名称 */
    private String projectName;

    /** 模块名称 */
    private Map<String,String> models = Maps.newHashMap();

    /** 包名 */
    private String basePackage;

    /** controller 映射路径*/
    private String mappingPath;

    private final String separator = File.separator;

    public ConfigurationBuilder(String basePath,String projectName,String basePackage){
        this.basePath = basePath;
        this.projectName = projectName;
        this.basePackage = basePackage;
    }

    public Configuration controllerConfiguration() {
        Configuration c = new Configuration();
        c.setTargettype(ConfigurationConstant.CONFIGURATION_CONTROLLER);
        c.setTargetObject(this.getFilePath(ConfigurationConstant.CONFIGURATION_CONTROLLER) + "/src/main/java");
        c.setTargetPackage(this.getPackage(ConfigurationConstant.CONFIGURATION_CONTROLLER)  + ".controller");
        c.setRootClass("com.zhy.generator.commons.base.BaseController");
        c.setMappingPath(this.mappingPath);
        c.setBeGenerator(true);
        return c;
    }

    public Configuration mapperConfiguration() {
        Configuration c = new Configuration();
        c.setTargettype(ConfigurationConstant.CONFIGURATION_MAPPER);
        c.setTargetObject(this.getFilePath(ConfigurationConstant.CONFIGURATION_MAPPER) + "/src/main/java");
        c.setTargetPackage(this.getPackage(ConfigurationConstant.CONFIGURATION_MAPPER)  + ".mapper");
        c.setRootInterface("com.zhy.generator.commons.base.BaseMapper");
        c.setBeGenerator(true);
        return c;
    }

    public Configuration sqlmapConfiguration() {
        Configuration c = new Configuration();
        c.setTargettype(ConfigurationConstant.CONFIGURATION_SQLMAP);
        c.setTargetObject(this.getFilePath(ConfigurationConstant.CONFIGURATION_SQLMAP) +  "/src/main/resources");
        c.setTargetPackage("mapper.curd");
        c.setBeGenerator(true);
        return c;
    }

    public Configuration poConfiguration() {
        Configuration c = new Configuration();
        c.setTargettype(ConfigurationConstant.CONFIGURATION_PO);
        c.setTargetObject(this.getFilePath(ConfigurationConstant.CONFIGURATION_PO) +  "/src/main/java");
        c.setTargetPackage(this.getPackage(ConfigurationConstant.CONFIGURATION_PO)  + ".po");
        c.setRootInterface("");
        c.setRootClass("com.zhy.generator.commons.base.BasePO");
        c.setBeGenerator(true);
        return c;
    }

    public Configuration voConfiguration() {
        Configuration c = new Configuration();
        c.setTargettype(ConfigurationConstant.CONFIGURATION_VO);
        c.setTargetObject(this.getFilePath(ConfigurationConstant.CONFIGURATION_VO) +  "/src/main/java");
        c.setTargetPackage(this.getPackage(ConfigurationConstant.CONFIGURATION_VO)  + ".vo");
        c.setRootInterface("");
        c.setRootClass("com.zhy.generator.commons.base.BaseVO");
        c.setBeGenerator(true);
        return c;
    }

    public Configuration serviceConfiguration() {
        Configuration c = new Configuration();
        c.setTargettype(ConfigurationConstant.CONFIGURATION_SERVICE);
        c.setTargetObject(this.getFilePath(ConfigurationConstant.CONFIGURATION_SERVICE) +  "/src/main/java");
        c.setTargetPackage(this.getPackage(ConfigurationConstant.CONFIGURATION_SERVICE)  + ".service");
        c.setRootClass("com.zhy.generator.commons.base.BaseService");
        c.setBeGenerator(true);
        return c;
    }

    public Configuration requestConfiguration() {
        Configuration c = new Configuration();
        c.setTargettype(ConfigurationConstant.CONFIGURATION_REQUEST);
        c.setTargetObject(this.getFilePath(ConfigurationConstant.CONFIGURATION_REQUEST) + "/src/main/java");
        c.setTargetPackage(this.getPackage(ConfigurationConstant.CONFIGURATION_REQUEST)  + ".request");
        c.setBeGenerator(true);
        return c;
    }

    public Configuration helperConfiguration() {
        Configuration c = new Configuration();
        c.setTargettype(ConfigurationConstant.CONFIGURATION_HELPER);
        c.setTargetObject(this.getFilePath(ConfigurationConstant.CONFIGURATION_HELPER) + "/src/main/java");
        c.setTargetPackage(this.getPackage(ConfigurationConstant.CONFIGURATION_HELPER)  + ".helper");
        c.setBeGenerator(true);
        return c;
    }

    public Configuration constantConfiguration() {
        Configuration c = new Configuration();
        c.setTargettype(ConfigurationConstant.CONFIGURATION_CONSTANT);
        c.setTargetObject(this.getFilePath(ConfigurationConstant.CONFIGURATION_CONSTANT) + "/src/main/java");
        c.setTargetPackage(this.getPackage(ConfigurationConstant.CONFIGURATION_CONSTANT) + ".constant");
        c.setBeGenerator(true);
        return c;
    }

    public String getBaseTarget() {
        return basePath;
    }

    public void setBaseTarget(String baseTarget) {
        this.basePath = baseTarget;
    }

    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public Map<String, String> getModels() {
        return models;
    }

    public String getModel(String model){
        return getModels().get(model);
    }

    public void setModels(Map<String, String> models) {
        this.models = models;
    }

    public String getMappingPath() {
        return mappingPath;
    }

    public void setMappingPath(String mappingPath) {
        this.mappingPath = mappingPath;
    }


    public String getPackage(String model){
        String modelName = getModel(model);
        String packageName = this.basePackage;
        if(StrUtil.isEmpty(modelName)){
            return packageName;
        }
        if(modelName.contains("-")){
            String str = modelName.substring(modelName.indexOf("-")+1, modelName.length());
            packageName += "."+str;
        }
        return packageName;
    }

    public String getFilePath(String model){
        String modelName = getModel(model);
        String path = this.basePath + this.separator + this.projectName;
        if (StrUtil.isNotBlank(modelName)){
            path += this.separator + modelName;
        }
        File f = new File(path);
        if(!f.isDirectory()){
            f.mkdirs();
        }

        return path;
    }
}
